package com.net.oil.main.fragment;


import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.base.BaseFragment;
import com.net.oil.main.adapter.NewsViewPagerAdapter;
import com.net.oil.main.bean.NewsTypeBean;
import com.net.oil.main.contract.NewsContract;
import com.net.oil.main.presenter.NewsPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 新闻fragment
 */
public class NewsFragment extends BaseFragment<NewsPresenter> implements NewsContract.View{

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.news_top_tab)
    TabLayout newsTopTab;
    @BindView(R.id.news_view_pager)
    ViewPager newsViewPager;
    Unbinder unbinder;

    private NewsViewPagerAdapter viewPagerAdapter;

    public static NewsFragment newInstance() {
        return new NewsFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        presenter = new NewsPresenter(this);
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        presenter.getNewsType();        //获取新闻类型
    }

    private void initView() {
        toolbarTitle.setText(R.string.news);
        toolbarGoBack.setVisibility(View.GONE);

        viewPagerAdapter = new NewsViewPagerAdapter(getActivity().getSupportFragmentManager());
        newsTopTab.setupWithViewPager(newsViewPager);
        newsViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(newsTopTab));
        newsViewPager.setAdapter(viewPagerAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取新闻类型成功
     * @param newsTypeBeanList  返回的数据类型
     */
    @Override
    public void getNewsTypeSuccess(List<NewsTypeBean> newsTypeBeanList) {
        if (newsTypeBeanList != null){
            viewPagerAdapter.updateDate(newsTypeBeanList);
        }
    }

    /**
     * 获取新闻类型失败
     */
    @Override
    public void getNewsTypeFailed() {

    }
}
